package work.MrSun.xyq.controller;

/**
 * @Author MrSun
 * @Date 2023/4/30 15:36
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import work.MrSun.xyq.model.XyqNewMessage;
import work.MrSun.xyq.model.XyqUser;
import work.MrSun.xyq.service.XyqNewMessageService;
import work.MrSun.xyq.service.XyqUserService;

/**
 * 管理员回复控制层
 */
@RestController
public class AddAdminReplyController {

    @Autowired
    private XyqUserService xyqUserService;

    @Autowired
    private XyqNewMessageService xyqNewMessageService;


    /**
     * 通过管理员添加最新消息
     *
     * @param adminId       管理员id
     * @param messageId     消息的id
     * @param messageUserId 消息对应用户的消息
     * @param newMessage    最新消息
     * @return
     */
    @Transactional
    @PostMapping("/addNewMessageByAdmin/{adminId}/{messageId}/{messageUserId}")
    public Integer addNewMessageByAdmin(@PathVariable Integer adminId,
                                        @PathVariable Integer messageId,
                                        @PathVariable Integer messageUserId,
                                        @RequestBody String newMessage) {

        //获取管理员
        XyqUser admin = xyqUserService.getById(adminId);
        //如果不是管理员
        if (admin.getUserIsAdmin() != 2) {
            return 400;
        }
        //初始化对象
        XyqNewMessage xyqNewMessage = new XyqNewMessage();
        xyqNewMessage.setNewMessageDetail(newMessage);
        xyqNewMessage.setMessageId(messageId);
        xyqNewMessage.setUserId(messageUserId);
        xyqNewMessage.setNewMessageType(3);
        //执行方法
        xyqNewMessageService.add(xyqNewMessage);
        //返回状态码
        return 200;
    }
}
